package com.lance.answer.gui.answer.gk;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.GkUserCourseInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 处理相关的数据信息
 */
@Slf4j
public class DealInfoUtil {

    public static void main(String[] args) throws Exception {

       System.err.println( DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+"艾").get("思想道德与法治"));


        Map<String, List<String>> result = countUserCourseInfo( "libs//userCourseInfo");
        int sum = 0;
        for(String key: result.keySet()){
            sum +=result.get(key).size();
            log.info("key:{},size:{}",key,result.get(key).size());
        }
        log.info("sum:{}",sum);


    }

    /**
     * 统计所有的学生和课程的信息，将一门课所有的学生放在一个文件里面
     */
    public static  Map<String, List<String>>  countUserCourseInfo(String filePath) {

        // key =课程名称  value = 用户名集合
        Map<String, List<String>> result = Maps.newHashMap();
        Map<String, List<String>> sortResult = Maps.newLinkedHashMap();
      try {
          for (File file : Objects.requireNonNull(new File(filePath).listFiles())) {

              String fileName = file.getName();
              // System.err.println("fileName:"+fileName);
              if(!fileName.contains(".json")){
                  continue;
              }
              String username = fileName.substring(0, fileName.length() - 5);

              List<GkUserCourseInfo> infoList = null ;
              try {
                  infoList=  JSON.parseArray(IOUtils.toString(new FileInputStream(file),
                          StandardCharsets.UTF_8), GkUserCourseInfo.class);
              }catch (Exception e){
                  e.printStackTrace();
              }
              if (infoList==null||infoList.isEmpty()) {
                  continue;
              }
              for (GkUserCourseInfo userCourseInfo : infoList) {
                  String courseName = userCourseInfo.getCourseName();
                  if (Strings.isNullOrEmpty(courseName)) {
                      continue;
                  }
                  List<String> users = result.computeIfAbsent(courseName, k -> Lists.newArrayList());
                  users.add(username);
              }

          }

          List<Map.Entry<String, List<String>>> sortList = new ArrayList<>(result.entrySet());
          Collections.sort(sortList, Comparator.comparingInt(o -> o.getValue().size()));
          for (Map.Entry<String, List<String>> entry : sortList) {
              sortResult.put(entry.getKey(), entry.getValue());
          }
      }catch (Exception e){
          e.printStackTrace();
      }

        return sortResult;
    }

}
